package StudentSystem.Mthod;

import StudentSystem.bean.User;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public abstract class UserMethod {
    // 生成随机验证码
    /*验证码规则：
     长度为5
     由4位大写或者小写字母和1位数字组成，同一个字母可重复
     数字可以出现在任意位置*/
    public static String checkCode() {
        StringBuilder checkCode = new StringBuilder();
        Random random = new Random();
        char[] Letter = new char[52];
        // 利用for循环遍历数组Letter，并把字母添加进数组
        for (int i = 0; i < Letter.length; i++) {
            if (i <= Letter.length / 2) {
                // a的Unicode编码为97
                Letter[i] = (char) (97 + i);
            } else {
                // A的Unicode编码为65
                Letter[i] = (char) (65 + i - 26);
            }
        }
        // 定义一个字符串类型用于接收前四位
        String code = "";
        for (int i = 0; i < 4; i++) {
            int randomIndex = random.nextInt(52);
            checkCode.append(Letter[randomIndex]);
        }
        checkCode.insert(random.nextInt(4), random.nextInt(10));
        return checkCode.toString();
    }

    // 查找用户并返回
    public static User findUser(ArrayList<User> users, String username) {
        for (User user : users) {
            if (username.equals(user.getUsername())) {
                return user;
            }
        }
        return new User();
    }

    // 清空数据
    public static void clearUserData() {
        File file = new File("E:\\Project-IDEA\\Study_java\\Java-basic\\src\\StudentSystem\\data\\UserData.txt");
        try (FileWriter fw = new FileWriter(file)) {
            fw.write("");
            fw.flush();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // 读取文件
    public static void readFile(ArrayList<User> users) {
        File f = new File("E:\\Project-IDEA\\Study_java\\Java-basic\\src\\StudentSystem\\data\\UserData.txt");
        try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
            String str;
            while ((str = reader.readLine()) != null) {
                String[] newStr = str.split(",");
                users.add(new User(newStr[0], newStr[1], newStr[2], newStr[3]));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // 写入用户文件
    public static void writeFile(User date) {
        // 写入文件
        File file = new File("E:\\Project-IDEA\\Study_java\\Java-basic\\src\\StudentSystem\\data\\UserData.txt");
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true))) {
            bw.write(date + "\n");
            bw.flush();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // 重载
    public static void writeFile(ArrayList<User> users) {
        // 写入文件
        File file = new File("E:\\Project-IDEA\\Study_java\\Java-basic\\src\\StudentSystem\\data\\UserData.txt");
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true))) {
            for (User user : users) {
                bw.write(user + "\n");
                bw.flush();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // 修改功能
    public static void replace(ArrayList<User> users) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.next();
        for (User user : users) {
            if (!(username.equals(user.getUsername()))) {
                System.out.println("该账号不存在。");
                users.clear();
                return;
            }
        }
        System.out.println("请输入身份证号码：");
        String identity = sc.next();
        System.out.println("请输入手机号：");
        String phoneNumber = sc.next();
        for (User user : users) {
            if (!(identity.equals(user.getIdentity()) && phoneNumber.equals(user.getPhoneNumber()))) {
                System.out.println("账号与信息不匹配，修改失败！！");
                users.clear();
                return;
            }
        }
        System.out.println("信息匹对成功，请输入要修改的密码值：");
        String newPassword = sc.next();
        User user = findUser(users, username);
        user.setPassword(newPassword);
        clearUserData();
        writeFile(users);
        System.out.println("修改成功！！");
        users.clear();
    }


    // 添加功能
    public static void register(ArrayList<User> users) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要注册的用户名（用户名长度必须在3~15位之间\n" +
                "\t\t只能是字母加数字的组合，但是不能是纯数字）:");
        String username = sc.next();
        System.out.println("请输入密码：");
        String password1 = sc.next();
        System.out.println("请再次输入密码：");
        String password2 = sc.next();
        System.out.println("请输入您的身份证号码");
        String identity = sc.next();
        System.out.println("请输入您的手机号：");
        String phoneNumber = sc.next();
        for (User user : users) {
            if (username.equals(user.getUsername())) {
                System.out.println("用户名重复，请重新注册");
                users.clear();
                return;
            }
        }
        if (!(username.matches("(?!^\\d+$)(?!^[a-zA-Z]+$)[a-zA-Z\\d]{3,15}"))) {
            System.out.println("您输入的用户名不符合规范");
            users.clear();
        } else if (!(password1.equals(password2))) {
            System.out.println("您两次输入的密码不一致！！请重新注册");
            users.clear();
        } else if (identity.length() != 18 || !(identity.matches("^[1-9]\\d{16}[x|X|[1-9]]$"))) {
            System.out.println("您输入的身份证信息不对！！");
            users.clear();
        } else if (!(phoneNumber.matches("^1\\d{10}"))) {
            System.out.println("您的手机号格式有误！！");
            users.clear();
        } else {
            User user = new User(username, password1, identity, phoneNumber);
            writeFile(user);
            System.out.println("注册成功");
            users.clear();
        }
    }


}
